package cn.tude.websocket.websocket.service.impl;


import cn.tude.websocket.websocket.mapper.MsgMapper;
import cn.tude.websocket.websocket.pojo.vo.MessageVO;
import cn.tude.websocket.websocket.pojo.vo.ShopVO;
import cn.tude.websocket.websocket.pojo.vo.UserVO;
import cn.tude.websocket.websocket.service.MsgSerivice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Slf4j
@Service
public class MsgSeriviceImpl implements MsgSerivice {

    @Autowired
    private MsgMapper msgMapper;

    @Override
    public List getPersons(Long id) {
        log.debug("查询曾经聊过天的人业务：用户id={}",id);
        List<MessageVO> msgList;

        msgList=msgMapper.getMsg(id);

        Set<Long> PersonSet=new HashSet<>();
        List<ShopVO> shopVOS=new ArrayList<>();
        List<UserVO> userVOS=new ArrayList<>();
        log.debug("messageVO={}",msgList);
        for (int i=0;i<msgList.size();i++){
            if (Objects.equals(msgList.get(i).getFromId(), id)){
                PersonSet.add(msgList.get(i).getToId());
            }else {
                PersonSet.add(msgList.get(i).getFromId());
            }
        }
        log.debug("shopSet={}",PersonSet);
        for (Long iid:PersonSet){
            if (id.toString().startsWith("10")){
                shopVOS.add(msgMapper.selectShop(iid));
            }else {
                userVOS.add(msgMapper.selectUser(iid));
            }
        }
        if (shopVOS.size()>0){
            return shopVOS;
        }
        return userVOS;
    }


    @Override
    public Object getPerson(Long id) {
        log.debug("查询人的信息业务：id={}",id);
        Object person;
        if (id.toString().startsWith("10")){
            person=msgMapper.selectUser(id);
        }else {
            person=msgMapper.selectShop(id);
        }
        return person;
    }
}
